package com.loubobooo.personal.blog.controller.portal;

import com.loubobooo.personal.blog.entity.vo.*;
import com.loubobooo.personal.blog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 文章分类目录的controller
 */
@Controller public class CategoryController {

    @Autowired private CategoryService categoryService;

    //根据分类查询文章
    @RequestMapping("/category/{cateId}")public ModelAndView ArticleListByCategoryView(
            @PathVariable("cateId") Integer cateId) {
        ModelAndView modelAndView = new ModelAndView();
        //设置每页显示条数
        int pageSize = 10;
        List<ArticleListVo> articleListVoList = categoryService
                .listArticleWithCategoryByPage(1, null, pageSize, cateId);

        //如果articleListVoList=null表示该分类不存在，如果=0表示该分类暂时没有文章
        modelAndView.addObject("articleListVoList", articleListVoList);

        //该分类信息
        CategoryVO categoryVO = categoryService.getCategoryById(1, cateId);
        modelAndView.addObject("categoryVO", categoryVO);

        modelAndView.setViewName("Home/Page/articleListByCategory");
        return modelAndView;
    }

    //根据分类查询文章分页
    @RequestMapping("/category/{cateId}/p/{pageNo}")
    public ModelAndView ArticleListByCategoryAndPageView(
            @PathVariable("pageNo") Integer pageNo, @PathVariable("cateId") Integer cateId) {
        ModelAndView modelAndView = new ModelAndView();
        //设置每页显示条数
        int pageSize = 10;
        List<ArticleListVo> articleListVoList = categoryService
                .listArticleWithCategoryByPage(1, pageNo, pageSize, cateId);
        modelAndView.addObject("articleListVoList", articleListVoList);
        modelAndView.setViewName("Home/Page/articleListByCategory");

        //该分类信息
        CategoryVO categoryVO = categoryService.getCategoryById(1, cateId);
        modelAndView.addObject("categoryVO", categoryVO);
        return modelAndView;
    }

}
